﻿using Clipper.BLL.DataBaseBLL;
using Clipper.Model.DataBaseModel;
using Communication;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserArmMoveControl.xaml 的交互逻辑
    /// </summary>
    public partial class UserArmMoveControl : UserControl
    {
        LocaltionPosition mlocaltionBll = new LocaltionPosition();

        public UserArmMoveControl()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            //Clipper.BLL.DataBaseBLL.LocaltionPosition mlocaltionBll = new BLL.DataBaseBLL.LocaltionPosition();
            //ObservableCollection<Clipper.Model.DataBaseModel.LocationPosition> locationModel = mlocaltionBll.GetModelList("");
            //foreach (LocationPosition model in locationModel)
            //{
            //    if (model.Place.Equals("普通样本"))
            //    {
            //        textBox.Text = model.YAxis + "";
            //        textBox1.Text = model.XAxis + "";
            //        textBox2.Text = model.Z1Axis + "";
            //        textBox4.Text = model.YOffset + "";
            //        textBox7.Text = model.XOffset + "";
            //    }



            //}
            
        }

        private void button21_Copy_Click(object sender, RoutedEventArgs e)
        {
            LocationPosition samplemodel = mlocaltionBll.GetModel("普通样本");
            textBox.Text = samplemodel.XAxis.ToString();
            textBox1.Text = samplemodel.YAxis.ToString();
            textBox2.Text = samplemodel.Z1Axis.ToString();
            textBox4.Text = samplemodel.XOffset.ToString();
            textBox7.Text = samplemodel.YOffset.ToString();


            LocationPosition urgentsamplemodel = mlocaltionBll.GetModel("紧急样本");

            textBox3.Text = urgentsamplemodel.XAxis.ToString();
            textBox5.Text = urgentsamplemodel.YAxis.ToString();
            textBox6.Text = urgentsamplemodel.Z1Axis.ToString();
            //textBox8.Text = urgentsamplemodel.XOffset.ToString();
            textBox9.Text = urgentsamplemodel.YOffset.ToString();
        }
        private void button6_Click(object sender, RoutedEventArgs e)
        {           

            #region  //修改普通样本的值
            LocationPosition upSampleModel = new LocationPosition();
            if (!textBox.Text.Equals(""))
            {
                upSampleModel.XAxis = Convert.ToInt32(textBox.Text);
            }
            else
            {
                //MessageBox.Show("普通样本X轴坐标不能为空！！","警告！");
                if (MessageBoxResult.Yes == MessageBox.Show("普通样本X轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }

            }

            if (!textBox1.Text.Equals(""))
            {
                upSampleModel.YAxis = Convert.ToInt32(textBox1.Text);
            }
            else
            {
                //MessageBox.Show("普通样本Y轴坐标不能为空！！", "警告！");
                if (MessageBoxResult.Yes == MessageBox.Show("普通样本Y轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox2.Text.Equals(""))
            {
                upSampleModel.Z1Axis= Convert.ToInt32(textBox2.Text);
            }
            else
            {
                //MessageBox.Show("普通样本Z轴坐标不能为空！！", "警告！");
                if (MessageBoxResult.Yes == MessageBox.Show("普通样本Z轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox4.Text.Equals(""))
            {
                upSampleModel.XOffset = Convert.ToInt32(textBox4.Text);
            }
            else
            {
                //MessageBox.Show("普通样本X轴偏移量不能为空！！", "警告！");
                if (MessageBoxResult.Yes == MessageBox.Show("普通样本X轴偏移量不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox7.Text.Equals(""))
            {
                upSampleModel.YOffset = Convert.ToInt32(textBox7.Text);
            }
            else
            {
                //MessageBox.Show("普通样本Y轴偏移量不能为空！!","警告！");
                if (MessageBoxResult.Yes == MessageBox.Show("普通样本Y轴偏移量不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            upSampleModel.Place = "普通样本";
            mlocaltionBll.Update(upSampleModel);

            #endregion

            #region  //修改紧急样本的值
            LocationPosition urgentSampleModel = new LocationPosition();
            if (!textBox3.Text.Equals(""))
            {
                urgentSampleModel.XAxis = Convert.ToInt32(textBox3.Text);
            }
            else
            {
                //MessageBox.Show("紧急样本X轴不能为空", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("紧急样本X轴不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox5.Text.Equals(""))
            {
                urgentSampleModel.YAxis = Convert.ToInt32(textBox5.Text);
            }
            else
            {
                //MessageBox.Show("紧急样本Y轴不能为空", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("紧急样本Y轴不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox6.Text.Equals(""))
            {
                urgentSampleModel.Z1Axis = Convert.ToInt32(textBox6.Text);
            }
            else
            {
                //MessageBox.Show("紧急样本Z轴不能空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("紧急样本Z轴不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox9.Text.Equals(""))
            {
                urgentSampleModel.YOffset = Convert.ToInt32(textBox9.Text);
            }
            else
            {
                //MessageBox.Show("紧急样本Y轴偏移量不能空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("紧急样本Y轴偏移量不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            urgentSampleModel.Place = "紧急样本";

            mlocaltionBll.Update(urgentSampleModel);

            #endregion
        }

        private void button22_Click(object sender, RoutedEventArgs e)
        {
            LocationPosition coldReagentmodel = mlocaltionBll.GetModel("常温试剂");
            textBox10.Text = coldReagentmodel.XAxis.ToString();
            textBox11.Text = coldReagentmodel.YAxis.ToString();
            textBox12.Text = coldReagentmodel.Z1Axis.ToString();
            textBox13.Text = coldReagentmodel.Z2Axis.ToString();
            textBox14.Text = coldReagentmodel.XOffset.ToString();
            textBox27.Text = coldReagentmodel.YOffset.ToString();
            //textBox20.Text = coldReagentmodel.Z1_z2Offset.ToString();

            LocationPosition ordinaryReagentmodel = mlocaltionBll.GetModel("冷试剂");

            textBox15.Text = ordinaryReagentmodel.XAxis.ToString();
            textBox16.Text = ordinaryReagentmodel.YAxis.ToString();
            textBox18.Text = ordinaryReagentmodel.Z1Axis.ToString();
            textBox17.Text = ordinaryReagentmodel.Z2Axis.ToString();
            textBox19.Text = ordinaryReagentmodel.XOffset.ToString();
            textBox28.Text = ordinaryReagentmodel.YOffset.ToString();

        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            #region //冷试剂保存 更新数据
            LocationPosition upColdReagentModel = new LocationPosition();
            if (!textBox10.Text.Equals(""))
            {
                upColdReagentModel.XAxis = Convert.ToInt32(textBox10.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂X轴坐标不能位空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂X轴坐标不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox11.Text.Equals(""))
            {
                upColdReagentModel.YAxis = Convert.ToInt32(textBox11.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂Y轴坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂Y轴坐标不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox12.Text.Equals(""))
            {
                upColdReagentModel.Z1Axis = Convert.ToInt32(textBox12.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂Z1轴坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂Z1轴坐标不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox13.Text.Equals(""))
            {
                upColdReagentModel.Z2Axis = Convert.ToInt32(textBox13.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂Z2坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂Z2轴坐标不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox14.Text.Equals(""))
            {
                upColdReagentModel.XOffset = Convert.ToInt32(textBox14.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂X轴偏移量不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂X轴偏移量不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox27.Text.Equals(""))
            {
                upColdReagentModel.YOffset = Convert.ToInt32(textBox27.Text);
            }
            else
            {
                //MessageBox.Show("冷试剂Y轴偏移量不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("常温试剂Y轴偏移量不能位空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            upColdReagentModel.Place = "常温试剂";
            mlocaltionBll.Update(upColdReagentModel);
            #endregion

            #region //保存更新常温试剂
            LocationPosition upOrdinaryReagentModel = new LocationPosition();
            if (!textBox15.Text.Equals(""))
            {
                upOrdinaryReagentModel.XAxis = Convert.ToInt32(textBox15.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区X轴坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区X轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox16.Text.Equals(""))
            {
                upOrdinaryReagentModel.YAxis = Convert.ToInt32(textBox16.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区Y轴坐标不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区Y轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox17.Text.Equals(""))
            {
                upOrdinaryReagentModel.Z2Axis = Convert.ToInt32(textBox17.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区Z1轴坐标不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区Z1轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox18.Text.Equals(""))
            {
                upOrdinaryReagentModel.Z1Axis = Convert.ToInt32(textBox18.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区Z2轴坐标不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区Z2轴坐标不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox19.Text.Equals(""))
            {
                upOrdinaryReagentModel.XOffset = Convert.ToInt32(textBox19.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区X轴偏移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区X轴偏移量不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox28.Text.Equals(""))
            {
                upOrdinaryReagentModel.YOffset = Convert.ToInt32(textBox28.Text);
            }
            else
            {
                //MessageBox.Show("常温试剂区Y轴偏移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("冷试剂区Y轴偏移量不能为空！！", "警告！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            upOrdinaryReagentModel.Place = "冷试剂";

            mlocaltionBll.Update(upOrdinaryReagentModel);
            #endregion
        }
        


        private void button_Click(object sender, RoutedEventArgs e)
        {
            //复位Z轴
            ArmMoveControl.ResetZAxisEnginner();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //复位 XY轴
            ArmMoveControl.ResetXYEnginner();
        }



        private void button5_Click(object sender, RoutedEventArgs e)
        {
            //定位到第一个孔位
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox.Text),Convert.ToInt32(textBox1.Text));

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            SampleAreaZ1DownDistance();
            sampleZ1Distance++;
        }
        //样本区Z1轴下移和上移
        int sampleZ1Distance = 1;
        public void SampleAreaZ1DownDistance()
        {
            if (sampleZ1Distance%2==0)
            {
                this.button2.Content = "下移";
                ArmMoveControl.Z1ReturnZeroEnginner();
            }
            else
            {
                this.button2.Content = "上移";
                ArmMoveControl.Z1DownDistanceEnginner(Convert.ToInt32(textBox2.Text));
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            //定位到紧急样本区
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox5.Text));
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            urgentSampleZ1downDistance();
            urgentSampleZ1Distance++;
        }

        //紧急样本区的下移
        int urgentSampleZ1Distance = 1;
        public void urgentSampleZ1downDistance()
        {
            if (urgentSampleZ1Distance%2==0)
            {
                this.button13.Content = "下移";
                ArmMoveControl.Z1ReturnZeroEnginner();
            }
            else
            {
                this.button13.Content = "上移";
                ArmMoveControl.Z1DownDistanceEnginner(Convert.ToInt32(textBox6.Text));
            }
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            //样品针定位到冷试剂区
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox10.Text), Convert.ToInt32(textBox11.Text)+Convert.ToInt32(textBox27.Text));
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            ordinaryReagentZ1downdistance();
            ordinaryReagent++;
        }
        int ordinaryReagent = 1;
        public void ordinaryReagentZ1downdistance()
        {
            if (ordinaryReagent%2==0)
            {
                this.button14.Content = "下移";
                ArmMoveControl.Z1ReturnZeroEnginner();
            }
            else
            {
                this.button14.Content = "上移";
                ArmMoveControl.Z1DownDistanceEnginner(Convert.ToInt32(textBox12.Text));
            }
        }


        //Z2定位到指定地点
        private void button11_Click(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox10.Text), Convert.ToInt32(textBox11.Text));
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            ordinaryZ2ReagentDownDistance();
            ordinaryZ2Reagent++;
        }

        //Z2下移到指定位置
        int ordinaryZ2Reagent = 1;
        public void ordinaryZ2ReagentDownDistance()
        {
            if (ordinaryZ2Reagent%2==0)
            {
                this.button16.Content = "下移";
                ArmMoveControl.Z2ReturnZeroEnginner();
            }
            else
            {
                this.button16.Content = "上移";
                ArmMoveControl.Z2DownDistanceEnginner(Convert.ToInt32(textBox13.Text));
            }
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text)+ Convert.ToInt32(textBox28.Text)*2);
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            coldZ1ReagentDownDistance();
            coldZ1Reagent++;
        }

        //Z1冷试剂区的下降
        int coldZ1Reagent = 1;
        public void coldZ1ReagentDownDistance()
        {
            if (coldZ1Reagent%2==0)
            {
                this.button15.Content = "下移";
                ArmMoveControl.Z1ReturnZeroEnginner();
            }
            else
            {
                this.button15.Content = "上移";
                ArmMoveControl.Z1DownDistanceEnginner(Convert.ToInt32(textBox18.Text));
            }
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            //冷试剂区Z2定位
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox15.Text), Convert.ToInt32(textBox16.Text));
        }

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            coldZ2ReagentDownDistance();
            coldZ2Reagent++;
        }
        //Z2轴下移步数
        int coldZ2Reagent = 1;
        public void coldZ2ReagentDownDistance()
        {
            if (coldZ2Reagent%2==0)
            {
                this.button17.Content = "下移";
                ArmMoveControl.Z2ReturnZeroEnginner();
            }
            else
            {
                this.button17.Content = "上移";
                ArmMoveControl.Z2DownDistanceEnginner(Convert.ToInt32(textBox17.Text));
            }
        }

    }
}
